#!/usr/bin/env bash

#######################################################################################
######################################0.0 常量定义######################################
### 系统环境生效
source /etc/profile
source ~/.bash_profile
### 静态文件部署目录
MADP_ADMIN_DIR=~/madp/madp-cp-oms
### NGINX端口常量
export NGINX_PORT=8888
export NGINX_BIN_DIR=~/local/nginx/sbin
#######################################################################################


#######################################################################################
######################################0.1 方法定义######################################
### nginx启动或重启
nginxReStart()
{
	cd $NGINX_BIN_DIR
    # 判断NGINX是否启动
    NGINX_PID=`ps -ef | grep -w nginx | grep -v "grep" | awk '{print $2}'`
    if [ "$NGINX_PID" == "" ]; then
        echo "[MADP-OMS] [nginx]开始启动--------------------------------------------------------"
        ./nginx
        echo "[MADP-OMS] [nginx]成功启动--------------------------------------------------------"
    else
        echo "[MADP-OMS] [nginx]开始重启--------------------------------------------------------"
        ./nginx -s reload
        echo "[MADP-OMS] [nginx]成功重启--------------------------------------------------------"
    fi
}

#######################################################################################
echo "[MADP-OMS] ----------------------------------------------------------------------"
# 1.1 进入工程目录
cd $PROJ_PATH/
# 1.2 删除打包目录
echo "[MADP-OMS] [dist]删除开始------------------------------------------------------------"
rm -rf dist
echo "[MADP-OMS] [dist]删除结束------------------------------------------------------------"
# 1.3 安全依赖
if [ ! -d "$PROJ_PATH/node_modules/" ];then
  echo "[MADP-OMS] [npm]依赖安装开始----------------------------------------------------------"
  npm run pre
  npm i
  echo "[MADP-OMS] [npm]依赖安装结束----------------------------------------------------------"
fi
# 1.4 打包编译
echo "[MADP-OMS] [dist]打包开始------------------------------------------------------------"
npm run build
echo "[MADP-OMS] [dist]打包结束------------------------------------------------------------"
# 1.5 删除部署目录文件
echo "[MADP-OMS] [dist]删除开始------------------------------------------------------------"
cd $MADP_ADMIN_DIR
rm -rf dist
echo "[MADP-OMS] [dist]删除结束------------------------------------------------------------"
# 1.6 拷贝打包文件
echo "[MADP-OMS] [dist]拷贝开始------------------------------------------------------------"
cp -rf $PROJ_PATH/dist $MADP_ADMIN_DIR/
echo "[MADP-OMS] [dist]拷贝结束------------------------------------------------------------"
# 1.7 重启nginx
echo "[MADP-OMS] ----------------------------------------------------------------------"
nginxReStart
echo "[MADP-OMS] ----------------------------------------------------------------------"

